package com.sdy.redis.service.impl;

import com.sdy.redis.driver.RedisDriver;
import com.sdy.redis.service.RedisService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Set;

/**
 * Created by zzq on 2017-08-23.
 */
@Slf4j
@Service
public class RedisServiceImpl implements RedisService {
    @Autowired
    private RedisDriver redisDriver;

    /**
     * 设置缓存对象
     *
     * @param key
     * @param value
     */
    @Override
    public void set(String key, Object value) {
        redisDriver.set(key, value);
    }

    @Override
    public void set(String key, Object value, int seconds) {
        redisDriver.set(key, value, seconds);
    }

    /**
     * 获取缓存对象
     *
     * @param key
     * @return
     */
    @Override
    public <T> T get(String key, Class<T> clazz) {
        return redisDriver.get(key, clazz);
    }

    @Override
    public <T> T getOrDefault(String key, Class<T> clazz, T defaultValue) {
        T v = redisDriver.get(key, clazz); 
        return v == null ? defaultValue : v;
    }

    @Override
    public Boolean setnx(String key, Object value) {
        return redisDriver.setnx(key, value);
    }

    @Override
    public Boolean setnx(String key, Object value, int seconds) {
        return redisDriver.setnx(key, value, seconds);
    }

    /**
     * 删除缓存对象
     *
     * @param key
     */
    @Override
    public Boolean del(String key) {
        return redisDriver.del(key);
    }

    @Override
    public void delRegx(String regx) {
        Set<String> keys = redisDriver.keys(regx);
        if (keys != null) {
            for (String s : keys) {
                redisDriver.del(s);
            }
        }
    }

    @Override
    public Boolean exists(String key) {
        return redisDriver.exists(key);
    }

    @Override
    public Long incr(String key) {
        return redisDriver.incr(key);
    }

    @Override
    public Long incr(String key, Long delta) {
        return redisDriver.incr(key, delta);
    }

    @Override
    public Long ttl(String key) {
        return redisDriver.ttl(key);
    }

    @Override
    public Boolean expire(String key, Integer seconds) {
        return redisDriver.expire(key, seconds);
    }

    @Override
    public Long sadd(String key, Object... value) {
        return redisDriver.sadd(key, value);
    }

    @Override
    public Long sremove(String key, Object... value) {
        return redisDriver.sremove(key, value);
    }

    @Override
    public Long scard(String key) {
        return redisDriver.scard(key);
    }

    @Override
    public Set<Object> smember(String key) {
        return redisDriver.smember(key);
    }

    @Override
    public Long rpush(String key, Object value) {
        return redisDriver.rpush(key, value);
    }

    @Override
    public Long llen(String key) {
        return redisDriver.llen(key);
    }

    @Override
    public List lrange(String key, int start, int end) {
        return redisDriver.lrange(key, start, end);
    }

    @Override
    public Set<String> keys(String regx) {
        return redisDriver.keys(regx);
    }
}
